const { ccclass, property } = cc._decorator;

import c from "./UiManage"
import u from "./MatchGameData"
import p from "./MatchGameConfig"
import d, { LayerType } from "./ObstacleConfig"
import h from "./MatchGameDataMgr"
import f from "./BottomBgCtrl"
import g from "./FlyPropellerCtrl"
import y from "./MatchItemCtrl"
import m from "./ObstacleCtrl"
import _ from "./PropsCtrl"
import v from "./TargetEditorItem";
import MatchGameConfig from "./MatchGameConfig";
import UiManager from "./UiManage";
enum s {
    MatchItem = 1,
    PropItem = 2,
    ObstacleItem = 3,
    Null = 4
}

@ccclass
export default class EditorCtrl extends cc.Component {
    public bottomBgParent = null;
    public lowestParent = null;
    public middleParents = null;
    public topParents = null;
    public bottomPrefab = null;
    public matchItemPrefab = null;
    public propItemPrefab = null;
    public obstacleItemPrefab = null;
    public emptyObstaclePrefab = null;
    public flyPropellerPrefab = null;
    public flyPropellerParent = null;
    public effectParent = null;
    public multiAreaParent = null;
    public multiObstacleItem = null;
    public cupboardPlate = null;
    public curtain = null;
    public flyMailPrefab = null;
    public icicle = null;
    public middleSquareGrid = [];
    public bottomSquareGrid = [];
    public topSquareGrid = [];
    public displayMapInfo = [];
    public _bottomArr = [];
    public _map_row = p.MAX_ROW;
    public _map_col = p.MAX_COL;
    public _mapWidth = MatchGameConfig.ITEM_WIDTH * p.MAX_ROW;
    public _mapHeight = MatchGameConfig.ITEM_HEIGHT * p.MAX_COL;
    public showTargetInfo = [];
    public targetPrefab = null;
    public targetItems = [];
    constructor() {
        super();
        var t = this;
        t.bottomBgParent = null;
        t.lowestParent = null;
        t.middleParents = null;
        t.topParents = null;
        t.bottomPrefab = null;
        t.matchItemPrefab = null;
        t.propItemPrefab = null;
        t.obstacleItemPrefab = null;
        t.emptyObstaclePrefab = null;
        t.flyPropellerPrefab = null;
        t.flyPropellerParent = null;
        t.effectParent = null;
        t.multiAreaParent = null;
        t.multiObstacleItem = null;
        t.cupboardPlate = null;
        t.curtain = null;
        t.flyMailPrefab = null;
        t.icicle = null;
        t.middleSquareGrid = [];
        t.bottomSquareGrid = [];
        t.topSquareGrid = [];
        t.displayMapInfo = [];
        t._bottomArr = [];
        t._map_row = p.MAX_ROW;
        t._map_col = p.MAX_COL;
        t._mapWidth = MatchGameConfig.ITEM_WIDTH * p.MAX_ROW;
        t._mapHeight = MatchGameConfig.ITEM_HEIGHT * p.MAX_COL;
        t.showTargetInfo = [];
        t.targetPrefab = null;
        t.targetItems = [];
        return t;
    }
    public async initData() {
        console.log("游戏初始化----");

        this.bottomPrefab = await UiManager.loaderPrefabInDeepPath("prefabs/bottomPrefab");
        this.matchItemPrefab = await UiManager.loaderPrefabInDeepPath("prefabs/matchItemPrefab");
        this.propItemPrefab = await UiManager.loaderPrefabInDeepPath("prefabs/propPrefab");
        this.obstacleItemPrefab = await UiManager.loaderPrefabInDeepPath("prefabs");
        this.emptyObstaclePrefab = await UiManager.loaderPrefabInDeepPath("prefabs/EmptyObstacleItem");
        this.flyPropellerPrefab = await UiManager.loaderPrefabInDeepPath("prefabs/flyPropellerPrefab");
        this.multiObstacleItem = await UiManager.loaderPrefabInDeepPath("prefabs/multiObstaclePrefab");
        this.cupboardPlate = await UiManager.loaderPrefabInDeepPath("prefabs/CupboardPlate");
        this.curtain = await UiManager.loaderPrefabInDeepPath("prefabs/Curtain");
        this.flyMailPrefab = await UiManager.loaderPrefabInDeepPath("prefabs/flyMailPrefab");
        this.icicle = await UiManager.loaderPrefabInDeepPath("prefabs/Icicle");

        f.ready(this.bottomPrefab, this.bottomBgParent);
        g.ready(this.flyPropellerPrefab, this.flyPropellerParent);

        await this.scheduleOnce(async () => { }, .1);
    }
    public _initSquareMap() {
        for (var e = 0; e < p.MAX_ROW; e++) {
            this.middleSquareGrid[e] = [];
            this.bottomSquareGrid[e] = [];
            this.displayMapInfo[e] = [];
            this.topSquareGrid[e] = [];
            this.displayMapInfo[e] = [];
            for (var t = 0; t < p.MAX_COL; t++) {
                this.middleSquareGrid[e][t] = null;
                this.bottomSquareGrid[e][t] = null;
                this.displayMapInfo[e][t] = null;
                this.topSquareGrid[e][t] = null;
                this.displayMapInfo[e][t] = 1;
            }
        }
    };
    public setDisplayMapInfo(e, t, a) {
        void 0 === a && (a = 0);
        this.displayMapInfo[e][t] = a;
        f.initBottomBgItem(e, t, this.displayMapInfo);
    };
    public loadBottomMapData(e) {
        this._map_row = e.length;
        this._map_col = e[0].length;
        for (var t = 0; t < this._map_row; t++) {
            this.displayMapInfo[t] = [];
            for (var a = 0; a < this._map_col; a++) {
                this.displayMapInfo[t][a] = e[t][a];
                f.initBottomBgItem(t, a, e);
            }
        }
    };
    public loadBottomData(e) {
        this._map_row = e.length;
        this._map_col = e[0].length;
        for (var t = 0; t < this._map_row; t++) for (var a = 0; a < this._map_col; a++) this.createBottomSquareItem(t, a, e[t][a]);
    };
    public loadMiddleData(e) {
        for (var t = 0; t < this._map_row; t++) for (var a = 0; a < this._map_col; a++) this.createMiddleSquareItem(t, a, e[t][a]);
        this.setBottomArr();
    };
    public setBottomArr() {
        this._bottomArr = [];
        for (var e = -1, t = -1, a = -1, o = 0; o < this._map_row; o++) {
            for (var i = 0, r = -1, n = -1, l = 0; l < this._map_col; l++) if (this.middleSquareGrid[o][l]) {
                -1 == r && (r = l);
                n = l;
                i++;
                (-1 == t || l < t) && (t = l);
                (-1 == a || l > a) && (a = l);
            }
            this._bottomArr[o] = {
                index1: r,
                count: i,
                index2: n
            };
            -1 !== r && -1 == e && (e = o);
        }
    };
    public loadTopData(e) {
        for (var t = 0; t < this._map_row; t++) for (var a = 0; a < this._map_col; a++) this.createTopSquareItem(t, a, e[t][a]);
        h.init();
    };
    public createBottomSquareItem(e, t, a) {
        var o = a.split("-"), i = o[0], r = o[1];
        i == p.SquareType.ObstacleItem && this.createObstacleItem(e, t, r, LayerType.Lowest);
    };
    public createMiddleSquareItem(e, t, a) {
        var o = a.split("-"), i = o[0], r = o[1];
        i == p.SquareType.MatchItem ? this.createMatchItem(e, t, Number(r)) : i == p.SquareType.PropItem ? this.createPropItem(e, t, Number(r)) : i == p.SquareType.ObstacleItem ? this.createObstacleItem(e, t, r, LayerType.Middle) : i == p.SquareType.Null ? console.log("---该地点为空--", e, t) : console.log("---配表报错--", i, r);
    };
    public createTopSquareItem(e, t, a) {
        var o = a.split("-"), i = o[0], r = o[1];
        i == p.SquareType.ObstacleItem && this.createObstacleItem(e, t, r, LayerType.Top);
    };
    public createMatchItem(e, t, a) {
        this.middleSquareGrid[e][t] || (this.middleSquareGrid[e][t] = y.createMatchItem(e, t, a));
    };
    public createObstacleItem(e, t, a, o) {
        m.createObstacleItem(e, t, a, o);
    };
    public createPropItem(e, t, a) {
        this.middleSquareGrid[e][t] = _.createPropItem(e, t, a);
    };
    public createObstacleTargetItem(e) {
        var t = cc.instantiate(this.targetPrefab);
        t.getComponent(v).init({
            squareType: p.SquareType.ObstacleItem,
            itemType: e,
            tootalCount: 1
        });
        this.targetItems.push(t);
    };
    public createMatchTargetItem(e) {
        var t = cc.instantiate(this.targetPrefab);
        t.getComponent(v).init({
            squareType: p.SquareType.MatchItem,
            itemType: e,
            tootalCount: 1
        });
        this.targetItems.push(t);
    };
    public createPropTargetItem(e) {
        var t = cc.instantiate(this.targetPrefab);
        t.getComponent(v).init({
            squareType: p.SquareType.PropItem,
            itemType: e,
            tootalCount: 1
        });
        this.targetItems.push(t);
    };
    public onCreateObstacle(e) {
        for (var t = this, a = 0; a < this.showTargetInfo.length; a++) if (this.showTargetInfo[a].itemType == e) {
            console.log("该目标已存在");
            this.showTargetInfo[a].targetCount += 1;
            return;
        }
        var o = u.getTargetObstacleArrByObstacle(e);
        if (!(o.length <= 0)) {
            this.showTargetInfo.push({
                targetSquareType: s.ObstacleItem,
                itemType: e,
                targetCount: 1
            });
            o.forEach((e) => {
                t.createObstacleTargetItem(e);
            });
        }
    };
    //     public ondelateObstacle() { };
    //     public createSquareItem() { };
    //     return r([C], t);
    // }(cc.Component));
}